﻿Public Module Globals

    Public g_mainwindow As MainWindow = Nothing
    Public g_mainpage As MainPage = Nothing

    Public WithEvents g_connection As Connection ' connection to the database
    'Public g_splashconnect As ConnectSplashForm ' shows connection being opened

    'Public g_controller As ViewController       ' handles view swapping in main form panels
    Public g_logpage As LogPage                  ' logging message display to the user
    Public g_album_mgr As AlbumManager           ' holds all album definitions
    Public g_currentalbum As Album               ' the current album
    Public g_currentphoto As Photo               ' the currently selected photo

    Public g_imageloader As ImageQueue           ' queues images to load, loads them into database

    Public g_sqlinspector As SqlInspectorPage    ' SQL Inspector form, shows SQL sent to database
    Public g_terminate As Boolean = False        ' should the main form close itself when loaded?
    Public g_coreevent_handler As CoreEventHandler ' handler for events from SpatialAlbum.Core

    Public Sub ApplicationStartup()

        g_currentphoto = New Photo
        g_imageloader = New ImageQueue
        g_sqlinspector = PageManager.Page(EnumPage.eSqlInspector)
        g_mainpage = PageManager.Page(EnumPage.eMain)
        g_logpage = PageManager.Page(EnumPage.eLog)
        Dim listener As New LogListener(g_logpage)
        Trace.Listeners.Add(listener)

        SendSettingsToCore()

        'g_controller = New ViewController
        g_connection = New Connection

        g_coreevent_handler = New CoreEventHandler

        g_album_mgr = New AlbumManager(g_connection)
        AddHandler g_album_mgr.StartAlbumLoad, AddressOf ReportingUtils.StartTimer
        AddHandler g_album_mgr.StartAlbumLoad, AddressOf OnStartAlbumLoad
        AddHandler g_album_mgr.AlbumLoaded, AddressOf OnAlbumLoaded

        'g_splashconnect = New ConnectSplashForm

        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException

        Connect()

    End Sub

    Public Sub Connect()
        g_connection.Server = My.Settings.CONNECT_Server
        g_connection.Database = My.Settings.CONNECT_Database
        g_connection.Auth = SpatialAlbum.Core.StringToAuth(My.Settings.CONNECT_Auth)
        g_connection.Login = My.Settings.CONNECT_Login
        g_connection.Password = SpatialAlbum.Core.DecryptPassword(My.Settings.CONNECT_Password)

        g_connection.Connect()

        If Not g_connection.Connected Then
            MessageBox.Show("You are not connected to the database.", "Not connected", MessageBoxButton.OK, MessageBoxImage.Warning)
        End If
    End Sub

    Private Sub SendSettingsToCore()
        SpatialAlbum.Core.Schema.Photo = My.Settings.TABLE_Photo
        SpatialAlbum.Core.Schema.Album = My.Settings.TABLE_Album
        SpatialAlbum.Core.Schema.TimeZone = My.Settings.TABLE_TimeZone
        SpatialAlbum.Core.Schema.City = My.Settings.TABLE_City
        SpatialAlbum.Core.Schema.County = My.Settings.TABLE_County
        SpatialAlbum.Core.Schema.State = My.Settings.TABLE_State
        SpatialAlbum.Core.Schema.Metro = My.Settings.TABLE_Metro
        SpatialAlbum.Core.Schema.ImageFilestreamColumn = My.Settings.COLUMN_ImageFilestream
        SpatialAlbum.Core.RuntimeSupport.AlbumData = My.Settings.RTS_AlbumData
        SpatialAlbum.Core.RuntimeSupport.CityData = My.Settings.RTS_CityData
        SpatialAlbum.Core.RuntimeSupport.CountyData = My.Settings.RTS_CountyData
        SpatialAlbum.Core.RuntimeSupport.StateData = My.Settings.RTS_StateData
        SpatialAlbum.Core.RuntimeSupport.MetroData = My.Settings.RTS_MetroData
        SpatialAlbum.Core.RuntimeSupport.TimeZoneData = My.Settings.RTS_TimeZoneData
    End Sub

    Private Sub SyncAlbumManager()
        If g_connection.Connected Then
            g_album_mgr.LoadAlbums()
        Else
            g_album_mgr.Clear()
        End If
    End Sub

    Public Sub TerminateProgram()
        g_terminate = True
        My.Application.Shutdown()
        'Application.Exit()
    End Sub

    Private Sub OnStartAlbumLoad()
        ReportingUtils.StartTimer()
        g_mainpage.SetStatusbarMessage("started to load album")
    End Sub

    Private Sub OnAlbumLoaded()
        ReportingUtils.ShowTimer("album loaded")
        g_mainpage.SetStatusbarMessage("album loaded")
    End Sub

    Public Sub OnUnhandledException(ByVal sender As Object, ByVal e As System.UnhandledExceptionEventArgs)
        If TypeOf e.ExceptionObject Is Exception Then
            ReportException(CType(e.ExceptionObject, Exception))
        Else
            MessageBox.Show("Unhandled AppDomain exception!")
        End If
    End Sub

    Public Sub ApplicationLoaded()
        If Not g_connection.Connected Then Return
        'CONVERT:g_mainform.RefreshAlbumList()
        g_album_mgr.LoadFirstAlbum()
    End Sub

    Private Sub g_connection_StartToConnect() Handles g_connection.StartToConnect
        'g_splashconnect.ShowForm(True)
        'g_splashconnect.Message = "connecting to " & g_connection.Database & " on " & g_connection.Server
        'My.Application.DoEvents()
    End Sub

    Private Sub g_connection_ConnectSucceeded() Handles g_connection.ConnectSucceeded
        'g_splashconnect.ShowForm(False)
    End Sub

    Private Sub g_connection_ConnectionAttemptFailed(ByVal ex As Exception) Handles g_connection.ConnectionAttemptFailed
        'g_splashconnect.ShowForm(False)
        ReportException(ex)
        MessageBox.Show("Connection failed: " & vbCrLf & vbCrLf & ex.Message, "Database Connection Failed", MessageBoxButton.OK, MessageBoxImage.Error)
    End Sub

    Private Sub g_connection_ConnectionChanged() Handles g_connection.ConnectionChanged
        SyncAlbumManager()
    End Sub

End Module
